package com.qifenqian.turbot.module.presenter

import android.content.Context
import com.qifenqian.turbot.base.BasePresenter
import com.qifenqian.turbot.module.contract.AccountContract
import com.qifenqian.turbot.module.entity.response.LoginEntity
import com.qifenqian.turbot.module.entity.response.RegisterEntity
import com.qifenqian.turbot.module.entity.response.UploadEntity
import com.qifenqian.turbot.module.entity.response.VersionEntity
import com.qifenqian.turbot.module.model.AccountModel
import com.qifenqian.turbot.net.ModelCallback

/**
 * @author H
 * @createTime 2018/12/28 17:47
 * @description
 * @see
 * @since
 */
class AccountPresenter(context: Context, view: AccountContract.View) : BasePresenter<AccountContract.View, AccountModel>(context, view), AccountContract.Presenter {

    override fun uploadFile(requestCode: Int, filePath: String) {
        mModel.uploadFile(filePath, object : ModelCallback<UploadEntity> {
            override fun onSuccess(msg: String?, t: UploadEntity?) {
                mView?.uploadFileResult(true, msg = msg, uploadEntity = t, requestCode = requestCode)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.uploadFileResult(false, msg = msg)
            }
        })
    }

    override fun logout(userCode: String) {
        mModel.logout(userCode, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.logoutResult(true, msg = msg)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.logoutResult(false, code, msg)
            }
        })
    }

    override fun giveFeedback(content: String) {
        mModel.giveFeedback(content, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.modifyPasswordResult(true)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.modifyPasswordResult(false, code, msg)
            }
        })
    }

    override fun modifyPassword(oldPassword: String, newPassword: String) {
        mModel.modifyPassword(oldPassword, newPassword, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.modifyPasswordResult(true)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.modifyPasswordResult(false, code, msg)
            }
        })
    }

    override fun modifyPhone(oldPhone: String, password: String, newPhone: String, smsCode: String) {
        mModel.modifyPhone(oldPhone, password, newPhone, smsCode, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.modifyPhoneResult(true)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.modifyPhoneResult(false, code, msg)
            }

        })
    }

    override fun createModel(): AccountModel {
        return AccountModel(mContext)
    }

    override fun checkVersion() {
        mModel.checkVersion(object : ModelCallback<VersionEntity> {
            override fun onSuccess(msg: String?, t: VersionEntity?) {
                mView?.checkVersionResult(true, versionEntity = t)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.checkVersionResult(false, code, msg)
            }
        })
    }

    override fun sendSmsCode(type: Int) {
        mModel.sendSmsCode(type, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.sendSmsCodeResult(true)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.sendSmsCodeResult(false, code, msg)
            }
        })
    }

    override fun login(userCode: String, password: String, loginType: Int, smsCode: String, showDialog: Boolean, versionEntity: VersionEntity?) {
        mModel.login(userCode, password, loginType, smsCode, showDialog, object : ModelCallback<LoginEntity> {
            override fun onSuccess(msg: String?, t: LoginEntity?) {
                mView?.loginResult(true, loginEntity = t!!, password = password)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.loginResult(false, code, msg)
            }
        })
    }

    override fun register(userCode: String, smsCode: String, password: String) {
        mModel.register(userCode, smsCode, password, object : ModelCallback<RegisterEntity> {
            override fun onSuccess(msg: String?, t: RegisterEntity?) {
                mView?.registerResult(true)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.registerResult(false, code, msg)
            }
        })
    }

    override fun identityVerifyFirst(userCode: String, frontPhotoOfIdCard: String, backPhotoOfIdCard: String, bankNo: String, bankName: String, phone: String, smsCode: String) {
        mModel.identityVerifyFirst(userCode, frontPhotoOfIdCard, backPhotoOfIdCard, bankNo, bankName, phone, smsCode, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.identityVerifyFirstResult(true)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.identityVerifyFirstResult(false, code, msg)
            }
        })
    }

    override fun identityVerifySecond(userCode: String, businessLicense: String, storePhotos: MutableList<String>, cateringServiceLicense: String) {
        mModel.identityVerifySecond(userCode, businessLicense, storePhotos, cateringServiceLicense, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.identityVerifySecondResult(true)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.identityVerifySecondResult(false, code, msg)
            }
        })
    }

    override fun resetPassword(userCode: String, password: String, smsCode: String) {
        mModel.resetPassword(userCode, password, smsCode, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.resetPasswordResult(true)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.resetPasswordResult(false, code, msg)
            }
        })
    }
}
